package org.example.cloudcommon.async.demo.mi;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;

@Configuration
public class ThreadPoolConfig {
    @Bean
    public ThreadPoolTaskExecutor threadPoolTaskExecutor(){
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        // 核心线程池数
        executor.setCorePoolSize(Runtime.getRuntime().availableProcessors() + 1);
        // 最大线程池数
        executor.setMaxPoolSize(Runtime.getRuntime().availableProcessors() * 2 +1);
        // 认为列表
        executor.setQueueCapacity(1000);
        executor.setRejectedExecutionHandler(new RejectedExecutionHandler() {
            @Override
            public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                // todo:1保存拒绝认为
                // todo:2通知
            }
        });
        executor.initialize();
        return executor;
    }

}
